package com.wu.store.service.impl;

import com.wu.store.dao.BrandDao;
import com.wu.store.entity.Brand;
import com.wu.store.service.BrandService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BrandServiceImpl implements BrandService {
	@Autowired
	private BrandDao brandDao;

	@Override
	public List<Brand> list(String keyword) {
		return brandDao.list(keyword);
	}

	@Override
	public boolean save(Brand brand) throws Exception {
		int affectedRow;
		if (StringUtils.isEmpty(brand.getName())) {
			throw new Exception("品牌名称不能为空");
		}
		if (brand.getId() == null) {
			affectedRow = brandDao.add(brand);
		} else {
			affectedRow = brandDao.update(brand);
		}
		if (affectedRow == 0) {
			throw new Exception("操作失败");
		}
		return true;
	}

}
